﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace BaseSimpleAPI
{
    /// <summary>
    /// 远程调用的帮助函数
    /// </summary>
    public class BaseControllerCallHelper
    {
        private BaseControllerCallHelper() { }

        private static BaseControllerCallHelper _BaseControllerCallHelper = new BaseControllerCallHelper();

        public static BaseControllerCallHelper GetInstance() { return _BaseControllerCallHelper; }

        /// <summary>
        /// 网络调用的函数
        /// </summary>
        public Func<string, object, string> NetCallFunc { get; set; }

        /// <summary>
        /// 网络调用
        /// </summary>
        /// <param name="url"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string NetCall(string url, object obj)
        {
            if (NetCallFunc != null)
                return NetCallFunc(url, obj);

            return string.Empty;
        }
    }

    /// <summary>
    /// 基础的调用函数
    /// </summary>
    /// <typeparam name="TEditModel"></typeparam>
    /// <typeparam name="TViewModel"></typeparam>
    public class BaseControllerCall<TEditModel, TViewModel>
        where TEditModel : BaseEditModel
        where TViewModel : BaseViewModel
    {
        public BaseControllerCall()
        {
            Controller = GetType().Name.Replace("Call", string.Empty);
        }

        /// <summary>
        /// 网络调用处理数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public StatusData NetCall<T>(string url, object data)
            where T : StatusData
        {
            string body = BaseControllerCallHelper.GetInstance().NetCall(url, data);

            if (string.IsNullOrEmpty(body))
                return StatusData.Error("Net error");

            return JsonConvert.DeserializeObject<T>(body);
        }

        /// <summary>
        /// 域
        /// </summary>
        public string Domain { get; set; } = ConfigurationManager.AppSettings["SystemData::Domain"];

        /// <summary>
        /// 服务范围
        /// </summary>
        public string Controller { get; set; }

        /// <summary>
        /// 获取网络连接
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public string GetUrl(string action) { return string.Join("/", new[] { Domain, Controller, action }); }

        /// <summary>
        /// 添加对应的接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual StatusData Add(TEditModel model)
        {
            return NetCall<StatusData<TViewModel>>(GetUrl("Add"), new { model = model });
        }

        /// <summary>
        /// 根据Id删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual StatusData RemoveById(long id)
        {
            return NetCall<StatusData>(GetUrl("RemoveById"), new { id = id });
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual StatusData Update(TEditModel model)
        {
            return NetCall<StatusData<TViewModel>>(GetUrl("Update"), new { model = model });
        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual StatusData GetItemById(long id)
        {
            return NetCall<StatusData<TViewModel>>(GetUrl("GetItemById"), new { id = id });
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public virtual StatusData GetAll()
        {
            return NetCall<StatusData<List<TViewModel>>>(GetUrl("GetAll"), null);
        }
    }
}
